/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.CacheEntryBy;
/*     */ import com.zimbra.common.account.Key.CosBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.CacheEntry;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import com.zimbra.cs.session.AdminSession;
/*     */ import com.zimbra.cs.session.Session.Type;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.ModifyAccountRequest;
/*     */ import com.zimbra.soap.admin.type.CacheEntryType;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ModifyAccount
/*     */   extends AdminDocumentHandler
/*     */ {
/*  57 */   private static final String[] TARGET_ACCOUNT_PATH = { "id" };
/*     */   
/*  59 */   protected String[] getProxiedAccountPath() { return TARGET_ACCOUNT_PATH; }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean domainAuthSufficient(Map context)
/*     */   {
/*  66 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean defendsAgainstDelegateAdminAccountHarvesting()
/*     */   {
/*  74 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  79 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  80 */     Provisioning prov = Provisioning.getInstance();
/*  81 */     ModifyAccountRequest req = (ModifyAccountRequest)JaxbUtil.elementToJaxb(request);
/*  82 */     AuthToken authToken = zsc.getAuthToken();
/*  83 */     String id = req.getId();
/*     */     
/*  85 */     Account account = prov.get(Key.AccountBy.id, id, authToken);
/*     */     
/*  87 */     Map<String, Object> attrs = req.getAttrsAsOldMultimap();
/*  88 */     defendAgainstAccountHarvesting(account, Key.AccountBy.id, id, zsc, attrs);
/*     */     
/*     */ 
/*  91 */     long curQuota = account.getLongAttr("zimbraMailQuota", 0L);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 114 */     checkQuota(zsc, account, attrs);
/*     */     
/*     */ 
/* 117 */     checkCos(zsc, account, attrs);
/*     */     
/*     */ 
/* 120 */     prov.modifyAttrs(account, attrs, true);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 125 */     account = prov.get(Key.AccountBy.id, id, zsc.getAuthToken());
/*     */     
/* 127 */     ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "ModifyAccount", "name", account.getName() }, attrs));
/*     */     
/*     */ 
/* 130 */     checkNewServer(zsc, context, account);
/*     */     
/* 132 */     long newQuota = account.getLongAttr("zimbraMailQuota", 0L);
/* 133 */     if (newQuota != curQuota)
/*     */     {
/* 135 */       AdminSession session = (AdminSession)getSession(zsc, Session.Type.ADMIN);
/* 136 */       if (session != null) {
/* 137 */         GetQuotaUsage.clearCachedQuotaUsage(session);
/*     */       }
/*     */     }
/*     */     
/* 141 */     Element response = zsc.createElement(AdminConstants.MODIFY_ACCOUNT_RESPONSE);
/* 142 */     ToXML.encodeAccount(response, account);
/* 143 */     return response;
/*     */   }
/*     */   
/*     */   static String getStringAttrNewValue(String attrName, Map<String, Object> attrs) throws ServiceException {
/* 147 */     Object object = attrs.get(attrName);
/* 148 */     if (object == null) object = attrs.get("+" + attrName);
/* 149 */     if (object == null) object = attrs.get("-" + attrName);
/* 150 */     if (object == null) { return null;
/*     */     }
/* 152 */     if (!(object instanceof String)) {
/* 153 */       throw ServiceException.PERM_DENIED("can not modify " + attrName + "(single valued attribute)");
/*     */     }
/* 155 */     return (String)object;
/*     */   }
/*     */   
/*     */   private void checkQuota(ZimbraSoapContext zsc, Account account, Map<String, Object> attrs) throws ServiceException {
/* 159 */     String quotaAttr = getStringAttrNewValue("zimbraMailQuota", attrs);
/* 160 */     if (quotaAttr == null) {
/*     */       return;
/*     */     }
/*     */     long quota;
/*     */     long quota;
/* 165 */     if (quotaAttr.equals(""))
/*     */     {
/* 167 */       quota = Provisioning.getInstance().getCOS(account).getIntAttr("zimbraMailQuota", 0);
/*     */     } else {
/*     */       try {
/* 170 */         quota = Long.parseLong(quotaAttr);
/*     */       } catch (NumberFormatException e) {
/* 172 */         throw AccountServiceException.INVALID_ATTR_VALUE("can not modify mail quota (invalid format): " + quotaAttr, e);
/*     */       }
/*     */     }
/*     */     
/* 176 */     if (!canModifyMailQuota(zsc, account, quota)) {
/* 177 */       throw ServiceException.PERM_DENIED("can not modify mail quota, domain admin can only modify quota if zimbraDomainAdminMaxMailQuota is set to 0 or set to a certain value and quota is less than that value.");
/*     */     }
/*     */   }
/*     */   
/*     */   private void checkCos(ZimbraSoapContext zsc, Account account, Map<String, Object> attrs) throws ServiceException {
/* 182 */     String newCosId = getStringAttrNewValue("zimbraCOSId", attrs);
/* 183 */     if (newCosId == null) {
/* 184 */       return;
/*     */     }
/*     */     
/* 187 */     Provisioning prov = Provisioning.getInstance();
/* 188 */     if (newCosId.equals(""))
/*     */     {
/* 190 */       Domain domain = prov.getDomain(account);
/* 191 */       if (domain != null) {
/* 192 */         newCosId = account.isIsExternalVirtualAccount() ? domain.getDomainDefaultExternalUserCOSId() : domain.getDomainDefaultCOSId();
/*     */         
/* 194 */         if (newCosId == null) {
/* 195 */           return;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 200 */     Cos cos = prov.get(Key.CosBy.id, newCosId);
/* 201 */     if (cos == null) {
/* 202 */       throw AccountServiceException.NO_SUCH_COS(newCosId);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 208 */     checkRight(zsc, cos, Rights.Admin.R_assignCos);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void checkNewServer(ZimbraSoapContext zsc, Map<String, Object> context, Account acct)
/*     */   {
/* 228 */     Server newServer = null;
/*     */     try {
/* 230 */       if (!Provisioning.onLocalServer(acct)) {
/* 231 */         newServer = Provisioning.getInstance().getServer(acct);
/*     */         
/*     */ 
/* 234 */         if (newServer != null) {
/* 235 */           SoapProvisioning soapProv = new SoapProvisioning();
/* 236 */           String adminUrl = URLUtil.getAdminURL(newServer, "/service/admin/soap/", true);
/* 237 */           soapProv.soapSetURI(adminUrl);
/* 238 */           soapProv.soapZimbraAdminAuthenticate();
/* 239 */           soapProv.flushCache(CacheEntryType.account, new Provisioning.CacheEntry[] { new Provisioning.CacheEntry(Key.CacheEntryBy.id, acct.getId()) });
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (ServiceException e)
/*     */     {
/* 245 */       ZimbraLog.mailbox.warn("cannot flush account cache on server " + (newServer == null ? "" : newServer.getName()) + " for " + acct.getName(), e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 251 */     relatedRights.add(Rights.Admin.R_assignCos);
/*     */     
/* 253 */     notes.add(String.format("All attrs provided in the attribute list have to settable by. the authed admin.   You can grant the %s right, which allows setting all attributes on %s, or grant the set attrs right just for the attributes the admin needs to set while creating an entry.", new Object[] { Rights.Admin.R_modifyAccount.getName(), "account" }) + "\n");
/*     */     
/*     */ 
/* 256 */     notes.add("Notes on zimbraCOSId: If setting zimbraCOSId, needs the " + Rights.Admin.R_assignCos.getName() + " right on the cos." + "If removing " + "zimbraCOSId" + ", needs the " + Rights.Admin.R_assignCos.getName() + " right on the domain default cos. (in domain attribute " + "zimbraDomainDefaultCOSId" + ").");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/ModifyAccount.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */